﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MoqEf;
using System.Collections.Generic;
using MoqEf.Tests.Models;
using System.Linq;

namespace MoqEf.Tests
{
    [TestClass]
    public class BasicTests
    {
        [TestMethod]
        public void BasicFunctionalityTests()
        {
            var moqContainer = new MoqEf.MoqContainer<TaskManagerContext>(new TaskManagerContext());
            ICollection<Person> people = new List<Person>
            {
                new Person{ Name = "Phil", PersonID = 1 },
                new Person{ Name = "John", PersonID = 2 }

            };
            ICollection<Task> tasks = new List<Task>
            {
                new Task{ TaskName = "Finish him!", TaskID = 1 },
                new Task{ TaskName = "Excellent!", TaskID = 1 },
            };
            moqContainer.AddMockDbSet(people, x => x.People).AddMockDbSet(tasks, x => x.Tasks);

            moqContainer.Context.People.Add(new Person { Name = "Lillard", PersonID = 100 });

            moqContainer.Context.SaveChanges();

            moqContainer.VerifyAddWasCalled<Person>().VerifySaveChangesWasCalled();

        }

        [TestMethod]
        public void AddEntityToSetTests()
        {
            var moqContainer = new MoqEf.MoqContainer<TaskManagerContext>(new TaskManagerContext());
            ICollection<Person> people = new List<Person>
            {
                new Person{ Name = "Phil", PersonID = 1 },
                new Person{ Name = "John", PersonID = 2 }

            };
            moqContainer.AddMockDbSet(people, x => x.People);

            moqContainer.Context.People.Add(new Person { Name = "Lillard", PersonID = 100 });

            moqContainer.Context.SaveChanges();

            moqContainer.VerifyAddWasCalled<Person>().VerifySaveChangesWasCalled();

            //should be able to retrieve the newly added element
            Assert.IsTrue(moqContainer.Context.People.Where(x => x.PersonID == 100).FirstOrDefault() != null);

        }
    }
}
